package com.hc.likelistenmusic.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

import com.hc.likelistenmusic.R;


public class VinylHandView extends View {
    Bitmap mVinylHandBitmap;
    Matrix mMatrix;
    float mCurrentDegree;
    float mOffset;
    float mScale = -1;
    float mDefaultMaxDegree = -25f;
    boolean mCurrentSprang;
    ValueAnimator mStartRotationAnimator;
    ValueAnimator mStopRotationAnimator;

    public VinylHandView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        mMatrix = new Matrix();
        LinearInterpolator linearInterpolator = new LinearInterpolator();
        mVinylHandBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.vinyl_hand);
        mOffset = mVinylHandBitmap.getWidth() * 0.18f;
        mStartRotationAnimator = ValueAnimator.ofFloat(0, mDefaultMaxDegree);
        mStartRotationAnimator.setDuration(500);
        mStartRotationAnimator.setInterpolator(linearInterpolator);
        mStartRotationAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mCurrentDegree = (float) animation.getAnimatedValue();
                if(mCurrentDegree == mDefaultMaxDegree){
                    mCurrentSprang = true;
                }
                invalidate();
            }
        });
        mStopRotationAnimator = ValueAnimator.ofFloat(mDefaultMaxDegree, 0);
        mStopRotationAnimator.setDuration(500);
        mStopRotationAnimator.setInterpolator(linearInterpolator);
        mStopRotationAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mCurrentDegree = (float) animation.getAnimatedValue();
                if(mCurrentDegree == 0){
                    mCurrentSprang = false;
                }
                invalidate();
            }
        });
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (w > 0 && h > 0) {
            int bitmapMaxBorder = Math.max(mVinylHandBitmap.getWidth(), mVinylHandBitmap.getHeight());
            if (bitmapMaxBorder > w || bitmapMaxBorder > h) {
                int viewMinBorder = Math.min(w, h);
                mScale = viewMinBorder / (float) bitmapMaxBorder;
            }
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(mScale!=-1){
            canvas.scale(mScale, mScale);
        }
        mMatrix.setRotate(mCurrentDegree, mOffset, mOffset);
        canvas.drawBitmap(mVinylHandBitmap, mMatrix, null);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mVinylHandBitmap != null) {
            mVinylHandBitmap.recycle();
            mVinylHandBitmap = null;
        }
        if (mStartRotationAnimator != null) {
            mStartRotationAnimator.removeAllListeners();
            mStartRotationAnimator.removeAllUpdateListeners();
            mStartRotationAnimator.cancel();
            mStartRotationAnimator = null;
        }
        if (mStopRotationAnimator != null) {
            mStopRotationAnimator.removeAllListeners();
            mStopRotationAnimator.removeAllUpdateListeners();
            mStopRotationAnimator.cancel();
            mStopRotationAnimator = null;
        }
    }

    public void startAnim() {
        if(mCurrentSprang){
            return;
        }
        mStartRotationAnimator.start();
    }

    public void stopAnim() {
        if(mCurrentSprang){
            mStopRotationAnimator.start();
        }
    }

    public void error(){
        if(mCurrentSprang){
            mStopRotationAnimator.start();
        }
    }
}
